package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Service("tbl_DKMH_CamHoacQuyDinhService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_DKMH_CamHoacQuyDinhService")
public class Tbl_DKMH_CamHoacQuyDinhServiceImpl implements Tbl_DKMH_CamHoacQuyDinhService,Serializable {

   private Log log = LogFactory.getLog(Tbl_DKMH_CamHoacQuyDinhServiceImpl.class);
   private static final long serialVersionUID = -8740095022264495229L;

   @Autowired
   @Qualifier("tbl_DKMH_CamHoacQuyDinhDAO")
   private Tbl_DKMH_CamHoacQuyDinhDAO _tbl_DKMH_CamHoacQuyDinhDao;

   @Autowired
   @Qualifier("tbl_DKMH_DotDKDAO")
   private Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao;

   @Autowired
   @Qualifier("tbl_ThongTin_HocVienDAO")
   private Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao;

   @Autowired
   @Qualifier("tbl_CTDT_MonHocDAO")
   private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

   @Autowired
   @Qualifier("tbl_CanBoDAO")
   private Tbl_CanBoDAO _tbl_CanBoDao;

   public final List<Tbl_DKMH_CamHoacQuyDinh> findAlltbl_DKMH_CamHoacQuyDinh(int pageSize, int page){
   List<Tbl_DKMH_CamHoacQuyDinh> result = get_tbl_DKMH_CamHoacQuyDinhDao().findAll(pageSize, page);
       log.info("Number of user in DB:: "+result.size());
       return result;
   }

   @Transactional(noRollbackFor=IllegalArgumentException.class)
   public final Long createtbl_DKMH_CamHoacQuyDinh(Tbl_DKMH_CamHoacQuyDinh obj){
       return _tbl_DKMH_CamHoacQuyDinhDao.save(obj);
   }

   public final void updatetbl_DKMH_CamHoacQuyDinh(Tbl_DKMH_CamHoacQuyDinh obj){
       _tbl_DKMH_CamHoacQuyDinhDao.update(obj);
   }//end update methode

   public final Tbl_DKMH_CamHoacQuyDinh gettbl_DKMH_CamHoacQuyDinh (Long id){
       log.info("Try to retrieve the tbl_DKMH_CamHoacQuyDinh: "+id);
       return get_tbl_DKMH_CamHoacQuyDinhDao().get(id);
   }

   public void deletetbl_DKMH_CamHoacQuyDinh(Long id){
       log.info("Try to delete the tbl_DKMH_CamHoacQuyDinh by Id : "+ id);
       if(_tbl_DKMH_CamHoacQuyDinhDao.exists(id)){
	    _tbl_DKMH_CamHoacQuyDinhDao.delete(id);
       } else {
       	throw new IllegalArgumentException("EmployeeId '"+id+"' does not exists");
       }
   }

   public void deleteListtbl_DKMH_CamHoacQuyDinh(String[] listId){
       for(String id : listId){
   	    deletetbl_DKMH_CamHoacQuyDinh(Long.parseLong(id));
       }
   }

   public List<Tbl_DKMH_CamHoacQuyDinh> findtbl_DKMH_CamHoacQuyDinhByCondition(List<ConditionReport> cons,int pageSize,int page){ 
       return _tbl_DKMH_CamHoacQuyDinhDao.findByCondition(Tbl_DKMH_CamHoacQuyDinh.class, cons, pageSize, page); 
   } 
 
   public int counttbl_DKMH_CamHoacQuyDinhByCondition(List<ConditionReport> cons){ 
       return _tbl_DKMH_CamHoacQuyDinhDao.countByCondition(Tbl_DKMH_CamHoacQuyDinh.class, cons); 
   } 
 
   public final Tbl_DKMH_CamHoacQuyDinhDAO get_tbl_DKMH_CamHoacQuyDinhDao() {
   	return _tbl_DKMH_CamHoacQuyDinhDao;
   }
   public final void set_tbl_DKMH_CamHoacQuyDinhDao(Tbl_DKMH_CamHoacQuyDinhDAO obj) {
   	this._tbl_DKMH_CamHoacQuyDinhDao = obj;
   }

   public Tbl_DKMH_DotDKDAO get_tbl_DKMH_DotDKDao() {
       return _tbl_DKMH_DotDKDao;
   }
   public void set_tbl_DKMH_DotDKDao(Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao) {
       this._tbl_DKMH_DotDKDao = _tbl_DKMH_DotDKDao;
   }

   public Tbl_ThongTin_HocVienDAO get_tbl_ThongTin_HocVienDao() {
       return _tbl_ThongTin_HocVienDao;
   }
   public void set_tbl_ThongTin_HocVienDao(Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao) {
       this._tbl_ThongTin_HocVienDao = _tbl_ThongTin_HocVienDao;
   }

   public Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
       return _tbl_CTDT_MonHocDao;
   }
   public void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao) {
       this._tbl_CTDT_MonHocDao = _tbl_CTDT_MonHocDao;
   }

   public Tbl_CanBoDAO get_tbl_CanBoDao() {
       return _tbl_CanBoDao;
   }
   public void set_tbl_CanBoDao(Tbl_CanBoDAO _tbl_CanBoDao) {
       this._tbl_CanBoDao = _tbl_CanBoDao;
   }

   public void createListtbl_DKMH_CamHoacQuyDinhByJSON(int dotDKId, String data, Tbl_TaiKhoan taiKhoan){
	   try {
			ChumanoLogger.init(Tbl_DKMH_CamHoacQuyDinhServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			List<Tbl_DKMH_CamHoacQuyDinh> tbl_DKMH_CamHoacQuyDinhs = new ArrayList<Tbl_DKMH_CamHoacQuyDinh>();

			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);
			for (int i = 0; i < jsons.size(); i++) {
				JSONObject obj = jsons.getJSONObject(i);
				String logStr ="";
				//-------------------------------------
				//Lay du lieu
				//MSSV	MaMonHoc	NhomHoc	TinhNang
				String mssv = obj.getString("MSSV");
				String maMH = obj.getString("MAMONHOC");
				
				String nhomhoc = obj.getString("NHOMHOC");
				String tingnang = obj.getString("TINHNANG");
				
				
				logStr ="[ " + (i+1)+ " - " 
				+ "MSSV: " + mssv + " - " 
				+ "MAMH: " + maMH + " - " 
				+ "NHOMHOC: " + nhomhoc + " - " 
				+ "TINHNANG: " + tingnang 
				+ "]";
				//=====================================
				// kiem tra DU LIEU TON TAI				
				//HOC VIEN
				List<Long> hocvienids = findHocVienId(mssv);
				if(hocvienids==null || hocvienids.size()==0)
				{
					ChumanoLogger.log(logStr + " <NOT MSSV>");
					continue;
				}
				Tbl_ThongTin_HocVien hocvien = new Tbl_ThongTin_HocVien();
				hocvien.setId(hocvienids.get(0));
				
				//MON HOC
				List<Long> monhocids = findMonHocId(maMH);
				if(monhocids==null || monhocids.size()==0)
				{
					ChumanoLogger.log(logStr + " <NOT MAMH>");
					continue;
				}
				Tbl_CTDT_MonHoc monhoc = new Tbl_CTDT_MonHoc();
				monhoc.setId(monhocids.get(0));
				
				Tbl_DKMH_DotDK dotdk = new Tbl_DKMH_DotDK();
				dotdk.setId(new Long(dotDKId));
				//-------------------------------------
				//=====================================
				Tbl_DKMH_CamHoacQuyDinh quydinh = new Tbl_DKMH_CamHoacQuyDinh();
				//tim ket qua da dang ky de cap nhat
				List<Long> qds = findQuyDinhDKMHId(dotdk.getId(), hocvien.getId(), monhoc.getId());
				if(qds!=null&&qds.size()>0){
					quydinh.setId( qds.get(0));
					logStr+= "< EXIST KQ>";
				}
				
				quydinh.setHocVienId(hocvien);
				quydinh.setMonHocId(monhoc);
				
				quydinh.setNhomHoc(nhomhoc);
				quydinh.setMaTinhNang(tingnang);
				
				quydinh.setDotDKId(dotdk);
				quydinh.setCanBoId(taiKhoan.getCanBoId());
				quydinh.setNgayCapNhat(new Date());
						
				//--------------------------------
				tbl_DKMH_CamHoacQuyDinhs.add(quydinh);
				
				ChumanoLogger.log(logStr);
			}// end for

			_tbl_DKMH_CamHoacQuyDinhDao.saveOrUpdateAll(tbl_DKMH_CamHoacQuyDinhs);
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " +e.getMessage());
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
		}
		   
		ChumanoLogger.stop();
   }
   
   private List<Long> findHocVienId(String ma){
	   List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
       {
       	 	ConditionReport con = new ConditionReport(); 
	       	con.setCol("MSSV");
	        con.setValue1(ma);
	        con.setOperator("eq");
	        cons.add(con);	
       }
       List<String> columns = new ArrayList<String>();
       columns.add("id");
       
	   List<Long> ids = _tbl_ThongTin_HocVienDao.findColumnByCondition(Tbl_ThongTin_HocVien.class,cons,columns, 1, 1);
	   return ids;
   }
   
   private List<Long> findMonHocId(String ma){
	   List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
       {
       	 	ConditionReport con = new ConditionReport(); 
	       	con.setCol("ma");
	        con.setValue1(ma);
	        con.setOperator("eq");
	        cons.add(con);	
       }
       List<String> columns = new ArrayList<String>();
       columns.add("id");
       
	   List<Long> ids = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class,cons,columns, 1, 1);
	   return ids;
   }
   
   private List<Long> findQuyDinhDKMHId(Long dotdkid, Long hocvienid, Long monhocid){
	   List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
       {
       	 	ConditionReport con = new ConditionReport(); 
	       	con.setCol("dotDKId.id");
	        con.setValue1(dotdkid);
	        con.setOperator("eq");
	        cons.add(con);	
       }
       {
      	 	ConditionReport con = new ConditionReport(); 
	       	con.setCol("hocVienId.id");
	        con.setValue1(hocvienid);
	        con.setOperator("eq");
	        cons.add(con);	
      }
       
       {
     	 	ConditionReport con = new ConditionReport(); 
	       	con.setCol("monHocId.id");
	        con.setValue1(monhocid);
	        con.setOperator("eq");
	        cons.add(con);	
     }
       List<String> columns = new ArrayList<String>();
       columns.add("id");
       
	   List<Long> ids = _tbl_DKMH_CamHoacQuyDinhDao.findColumnByCondition(Tbl_DKMH_CamHoacQuyDinh.class,cons,columns, 1, 1);
	   return ids;
   }

}//endclass

